﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Entities;
using System.Data.SqlClient;

namespace DAL
{
    public class CoachOfScheduleDAL : BaseObject
    {
        #region Constructor

        ///<summary>
        ///Initializes a new instance of the <see cref="UserDal"/> 
        ///class with the specified <see cref="BaseDAL"/>.
        ///</summary>
        public CoachOfScheduleDAL()
            : base()
        {

        }

        #endregion

        #region Public Methods
        ///<summary>
        ///Create Obj from ireader.
        ///</summary>

        private CoachOfSchedule GetObjectFromReader(IDataReader reader)
        {
            CoachOfSchedule temp = new CoachOfSchedule();
            temp.CoachID = (reader["CoachID"] is DBNull) ? int.MinValue : (System.Int32)reader["CoachID"];
            temp.ScheduleID = (reader["ScheduleID"] is DBNull) ? int.MinValue : (System.Int32)reader["ScheduleID"];
            return temp;
        }

        ///<summary>
        ///Get List CoachOfSchedule and store in a list 
        ///</summary>
        public List<CoachOfSchedule> GetList_CoachOfSchedule(string whereSql, string orderBy)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@WhereCondition", whereSql));
                cmd.Parameters.Add(new SqlParameter("@OrderByExpression", orderBy));
                conn.Open();
                List<CoachOfSchedule> lst = new List<CoachOfSchedule>();
                using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    while (reader.Read())
                    {
                        lst.Add(GetObjectFromReader(reader));
                    }
                }
                conn.Close();
                return lst;
            }
        }

        ///<summary>
        ///Get All CoachOfSchedule and store in a list 
        ///</summary>
        public List<CoachOfSchedule> GetAll_CoachOfSchedule()
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                conn.Open();
                List<CoachOfSchedule> lst = new List<CoachOfSchedule>();
                using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    while (reader.Read())
                    {
                        lst.Add(GetObjectFromReader(reader));
                    }
                }
                conn.Close();
                return lst;
            }
        }

        ///<summary>
        ///Get List CoachOfSchedule by UserName
        ///</summary>
        public CoachOfSchedule Get_CoachOfSchedule_By_ID(int CoachID, int ScheduleID)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@CoachID", CoachID));
                cmd.Parameters.Add(new SqlParameter("@ScheduleID", ScheduleID));
                CoachOfSchedule temp = new CoachOfSchedule();
                using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    if (reader.Read())
                    {
                        temp = GetObjectFromReader(reader);
                    }
                }
                conn.Close();
                return temp;
            }
        }

        ///<summary>
        ///Using for paging data 
        ///</summary>
        /// <param name="pageIndex">Number of pages</param>
        /// <param name="PageSize">Number of a rows per page</param>
        /// <param name="conditions">Where  fields</param>
        /// <param name="groupBy">Group by fields</param>
        public DataTable Get_CoachOfScheduleAsPaging(int pageIndex, int pageSize, string sSortColumn, string sFields, string sWhere, string sGroupBy)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
                cmd.Parameters.Add(new SqlParameter("@PageSize", pageSize));
                cmd.Parameters.Add(new SqlParameter("@SortColumn", sSortColumn));
                cmd.Parameters.Add(new SqlParameter("@Fields", sFields));
                cmd.Parameters.Add(new SqlParameter("@Where", sWhere));
                cmd.Parameters.Add(new SqlParameter("@Group", sGroupBy));
                SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
                try
                {
                    conn.Open();
                    DataTable tbl = new DataTable();
                    dataAdapter.Fill(tbl);
                    conn.Close();
                    return tbl;
                }
                catch
                {
                    return null;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        ///<summary>
        ///Insert new CoachOfSchedule
        ///</summary>
        public void Insert_CoachOfSchedule(CoachOfSchedule temp)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand(usp_InsertCoachOfSchedule, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@CoachID", temp.CoachID));
                cmd.Parameters.Add(new SqlParameter("@ScheduleID", temp.ScheduleID));

                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        ///<summary>
        ///Update a CoachOfSchedule
        ///</summary>
        public void Update_CoachOfSchedule(CoachOfSchedule temp)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@CoachID", temp.CoachID));
                cmd.Parameters.Add(new SqlParameter("@ScheduleID", temp.ScheduleID));
                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        public void Delete_CoachOfSchedule(CoachOfSchedule temp)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@CoachID", temp.CoachID));
                cmd.Parameters.Add(new SqlParameter("@ScheduleID", temp.ScheduleID));
                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        #endregion
    }
}
